package com.xcexample.electronicclasscard;

import static android.content.Context.NOTIFICATION_SERVICE;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import com.xcexample.electronicclasscard.log.LogUtils;
import com.xcexample.electronicclasscard.main.MainActivity;

/***
 * 开机启动的广播
 */
public class BootCompleteReceiver extends BroadcastReceiver {

    private static final String BOOT_ACTION = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        LogUtils.e("onReceive: Boot Complete 开机启动的广播接收者");
        if (BOOT_ACTION.equals(intent.getAction())) {
            openActivity(context);
        } else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
            openActivity(context);
        } else if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
            LogUtils.e("ACTION_PACKAGE_ADDED");
            openActivity(context);
        }

    }


    public void openActivity(Context context) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            startActivityVersionHeight(context);
        } else {
            startActivityVersionLower(context);
        }
        LogUtils.e("openActivity: 启动Activity");
    }


    public void startActivityVersionLower(Context context) {
        Intent myIntent = new Intent(context, MainActivity.class);
        myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(myIntent);
    }


    public void startActivityVersionHeight(Context context) {

        Intent intent1 = new Intent(context, MainActivity.class);
        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(context, 0,
                intent1, PendingIntent.FLAG_UPDATE_CURRENT);

        String channelID = "my_channel_ID";
        String channelNAME = "my_channel_NAME";
        int level = NotificationManager.IMPORTANCE_HIGH;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationManager manager = (NotificationManager)
                    context.getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);
            manager.createNotificationChannel(channel);
        }
        NotificationCompat.Builder notification =
                new NotificationCompat.Builder(context, channelID)
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setContentTitle("Start Activity")
                        .setContentText("click me")
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .setCategory(NotificationCompat.CATEGORY_CALL)
                        .setAutoCancel(true)
                        .setFullScreenIntent(fullScreenPendingIntent, true);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.notify(100, notification.build());
    }
}

